/**
 * @author: zzj
 * @date: 2020-03-23 14:08:32
 * @version: 1.0
 */
import {getStore} from "js-utils";

export function setMenu(menu) {
  return menu.map(el => {
    const {
      id,
      path,
      menuName,
      icon,
      children,
      parentId,
      hideFlag
    } = el;
    return {
      path: path || '/' + id,
      component: (resolve) => {
        if (!path) {
          if (parentId === "0") {
            require(['@/views/index/index'], resolve)
          } else {
            require(['@/views/routerView/index'], resolve)
          }
        } else {
          require([`@/views${path}`], resolve)
        }
      },
      //name:name,
      meta: {
        name: menuName,
        icon: icon,
        hideFlag: hideFlag,
        keepAlive: false
      },
      children: (children && children.length) ? setMenu(children) : []
    }
  })
}

/**
 * 获取小图标列表
 */
export function getIconList() {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4 && xhr.status === 200) {
        let iconString = xhr.responseText.replace(/@font-face {[^}]+}/, '');
        let iconList = iconString.match(/icon-[^:]+/ig);
        resolve(iconList);
      }
    };
    xhr.open('GET', '//at.alicdn.com/t/font_904872_39lanr15pjp.css');
    xhr.send();
  });
}

export function getToken() {
  let token = getStore("BIZ_TOKEN", true);
  return token ? token : null;
}


export function getRandomKey(length = 12) {
  const letterStr = "0123456789abcdefghijklmnopqrstuvwxyz";
  let str = "";
  for (let i = 0; i < length; i++) {
    str += letterStr[(Math.random() * 35 | 0)];
  }
  return str;
}

